﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaBrowser.Library.Entities;
using MusicPlugin.Util;
using MusicPlugin.LibraryManagement;
using MediaBrowser.Library.Logging;
using MediaBrowser.Library.Persistance;
using MediaBrowser.Library.Extensions;

namespace MusicPlugin.Library.Entities
{
    public class MusicPluginFolder: Folder
    {

        List<BaseItem> _items;
        protected override List<BaseItem> ActualChildren
        {
            get
            {
                _items = base.ActualChildren;
                if (Settings.Instance.ShowPlaylistAsFolder)
                {
                    if (_items != null) 
                    {
                        Folder folder = MusicHelper.GetPlaylistFolder();
                        if (folder != null)
                        {
                            if (!_items.Contains(folder))
                                _items.Add(folder);
                        }
                    }
                }

                return _items;
            }
        }

        string _name;
        public override string Name
        { 
            get
            { 
                if (Settings.Instance.LoadNormalLibrary)
                    return Settings.Instance.NormalLibraryVirtualFolderName;
                return _name;
            }
            set 
            {
                base.Name = value;
                _name = value;
            }
        }

        public override string PrimaryImagePath
        {
            get
            {
                if (!string.IsNullOrEmpty(Settings.Instance.NormalLibraryIcon))
                    return Settings.Instance.NormalLibraryIcon;
                return base.PrimaryImagePath;
            }
            set
            {
                base.PrimaryImagePath = value;
            }
        }

        //public override void ValidateChildren()
        //{
        //    base.ValidateChildren();

            //if (_itemsGenre == null)
            //{
            //    _itemsGenre = new Dictionary<string, Folder>();
            //    if (Genre == null)
            //        Genre = new Folder();
            //    Genre.Name = "Genre";
            //    Genre.Id = "Genre".GetMD5();
            //    if (!items.Contains(Genre))
            //        items.Add(Genre);
            //}

            //var baseItems = base.RecursiveChildren.Select(a => a as Song).Where(a => a is Song);
            //Logger.ReportInfo("Justin" + baseItems.Count().ToString());

            //foreach (var item in baseItems)
            //{
            //    string genre = item.Genres[0];
            //    if (!string.IsNullOrEmpty(genre))
            //    {
            //        if (_itemsGenre.ContainsKey(genre))
            //            _itemsGenre[genre].Children.Add(item);
            //        else
            //        {
            //            var folder = new Folder() { Name = genre };
            //            Logger.ReportInfo("Justin adding" + genre);
            //            _itemsGenre.Add(genre, folder);
            //            Genre.Children.Add(folder);
            //        }
            //    }
            //}
        //}
    }
}
